package com.yidusoft.controller;

import com.yidusoft.core.Result;
import com.yidusoft.core.ResultGenerator;
import com.yidusoft.domain.SecRoleMember;
import com.yidusoft.domain.SecUser;
import com.yidusoft.service.SecRoleMemberService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidusoft.service.SecUserService;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import java.util.*;

/**
* Created by CodeGenerator on 2017/07/18.
*/
@Controller
@RequestMapping("/sec/role/member")
public class SecRoleMemberController {
    @Resource
    private SecRoleMemberService secRoleMemberService;

    /**
     * 跳转到成员添加页面
     * @param id
     * @param roleName
     * @param model
     * @return
     */
    @RequestMapping(value ="/linkAdd")
    public String linkAdd(String id,String roleName,Model model){
        model.addAttribute("id",id);
        model.addAttribute("roleName",roleName);
        return  "secrolemember/secrolemember-add";
    }

    /**
     * 跳转到成员列表页面
     * @param id
     * @param roleName
     * @param model
     * @return
     */
    @RequestMapping(value ="/linkList")
    public String linkList(String id,String roleName,Model model){
        model.addAttribute("id",id);
        model.addAttribute("roleName",roleName);
        return  "secrolemember/secrolemember-list";
    }

    @RequestMapping(value ="/linkUpdate")
    public String linkUpdate(Model model,String id){
        SecRoleMember secRoleMember = secRoleMemberService.findById(id);
        model.addAttribute("secRoleMember",secRoleMember);
        return  "secrolemember/secrolemember-update";
    }

    @Resource
    private SecUserService secUserService;

    /**
     * 查询出角色的成员列表
     * @param id
     * @return
     */
    @PostMapping("/memberList")
    @ResponseBody
    public Map<String,Object> memberListlist(String id) {
        Map<String,Object> map = new HashMap<String,Object>();
        List<SecUser> list = secUserService.userListByRoleId(id);
        map.put("Rows",list);
        map.put("Total",list.size());
        return map;
    }

    /**
     * 查询用户列表
     * @param id
     * @return
     */
    @PostMapping("/userList")
    @ResponseBody
    public Map<String,Object> userListlist(String id,String orgId) {
        System.out.println("id:"+orgId);
        Map<String,Object> map = new HashMap<String,Object>();
        Map<String,Object> parmMap = new HashMap<String,Object>();
        parmMap.put("id",id);
        parmMap.put("orgId",orgId);
        List<SecUser> list = secUserService.roleChooseUser(parmMap);
        map.put("Rows",list);
        map.put("Total",list.size());
        return map;
    }

    /**
     * 批量添加成员
     * @param secRoleMember
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Result add(SecRoleMember secRoleMember) {
        String arr [] = (secRoleMember.getUserId()).split(",");
        for(String str : arr) {
            secRoleMember.setUserId(str);
            secRoleMember.setId(UUID.randomUUID().toString());
            secRoleMemberService.save(secRoleMember);
        }
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 添加角色
     * @param secRoleMember
     * @return
     */
    @PostMapping("/addRoles")
    @ResponseBody
    public Result addRoles(SecRoleMember secRoleMember) {
        String arr [] = (secRoleMember.getRoleId()).split(",");
        for(String str : arr) {
            secRoleMember.setRoleId(str);
            secRoleMember.setId(UUID.randomUUID().toString());
            secRoleMemberService.save(secRoleMember);
        }
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 添加部门
     * @param secUser
     * @return
     */
    @PostMapping("/addOrg")
    @ResponseBody
    public void addOrg(SecUser secUser,String roleId) {
        if(secUser!=null && roleId!=null && roleId!="") {
            String arr [] = secUser.getOrgId().split(",");
            for(String orgId : arr) {
                secUser.setOrgId(orgId);
                List<SecUser> userList = secUserService.getQueryAll(secUser);
                SecRoleMember secRoleMember  = new SecRoleMember();
                for( SecUser sec : userList){
                    secRoleMember.setId(UUID.randomUUID().toString());
                    secRoleMember.setUserId(sec.getId());
                    secRoleMember.setRoleId(roleId);
                    secRoleMemberService.save(secRoleMember);
                }
            }
        }
    }
   //把数据永久批量删除
    @PostMapping("/deleteTrue")
    @ResponseBody
    public Result deleteTrue(SecRoleMember secRoleMember) {
        String arr [] = secRoleMember.getUserId().split(",");
        System.out.println("userId:"+secRoleMember.getUserId());
        System.out.println("roleId:"+secRoleMember.getRoleId());
        for(String str : arr) {
            System.out.println("str:"+str);
            secRoleMember.setUserId(str);
            secRoleMemberService.deleteUserIdAndRoleId(secRoleMember);
        }
        return ResultGenerator.genSuccessResult();
    }

    //逻辑删除，数据还在
    @PostMapping("/delete")
    @ResponseBody
    public Result delete(String  id) {
        SecRoleMember secRoleMember = secRoleMemberService.findById(id);
//        secRoleMember.setDeleted(1);
        secRoleMemberService.update(secRoleMember);
        return ResultGenerator.genSuccessResult();
    }


    //逻辑删除，数据还在 批量删除
    @PostMapping("/deleteBacth")
    @ResponseBody
    public Result deleteBacth(String  ids) {
        String arr [] = ids.split(",");
        for(String str : arr){
            secRoleMemberService.deleteById(str);
        }
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    @ResponseBody
    public Result update(SecRoleMember secRoleMember) {
        secRoleMemberService.update(secRoleMember);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    @ResponseBody
    public Result detail(@PathVariable String id) {
        SecRoleMember secRoleMember = secRoleMemberService.findById(id);
        return ResultGenerator.genSuccessResult(secRoleMember);
    }

    @PostMapping("/listByPage")
    @ResponseBody
    public Result listByPage(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<SecRoleMember> list = secRoleMemberService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
